package com.muyu.controller;

import com.muyu.api.entity.ResponseEntity;
import com.muyu.api.response.ResultResponse;
import com.muyu.service.CommonService;
import com.muyu.service.UserQuestionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: CommonController
 * @Author: Zirun.Xin
 * @Date: 2025/4/29 下午2:33
 * @Version: 1.0
 * @Description: 公共控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api/v1/common")
public class CommonController {

    /**
     * 公共业务层
     */
    private final CommonService commonService;

    /**
     * 问题反馈业务层
     */
    private final UserQuestionsService userQuestionsService;

    /**
     * 所有问题反馈截图导出zip(压缩包)
     *
     * @param response http响应
     */
    @GetMapping(value = "/export-questions-images")
    public void exportQuestionsImages(HttpServletResponse response) {
        commonService.exportQuestions(response);
    }

    /**
     * 根据图片路径进行图片预览
     *
     * @param imgPath   图片路径
     * @param accountId 账户id
     */
    @GetMapping(value = "/review-img-by-path")
    public void reviewImgByPath(@RequestParam("imgPath") String imgPath, @RequestParam("accountId") String accountId) {
        userQuestionsService.reviewImgByPath(imgPath, accountId);
    }

    /**
     * 根据图片地址进行下载
     *
     * @param imgPath   图片地址
     * @param accountId 账户id
     */
    @GetMapping(value = "/download-img-by-path")
    public void downloadImgByPath(@RequestParam("imgPath") String imgPath,
                                  @RequestParam("accountId") String accountId) {
        userQuestionsService.downloadImgByPath(imgPath, accountId);
    }

    /**
     * 保存用户问题及关联图片
     *
     * @param images           上传的图片文件数组
     * @param creatorName      创建者姓名
     * @param creatorAccountId 创建者账号ID
     * @param creatorDept      创建者部门
     * @param questionContent  问题内容
     * @param questionType     问题类型
     * @return 响应结果集
     */
    @PostMapping(value = "/save")
    public ResponseEntity<ResultResponse<Boolean>> saveQuestion(
            @RequestParam(value = "image") MultipartFile[] images,
            @RequestParam(value = "creatorName") String creatorName,
            @RequestParam(value = "creatorAccountId") String creatorAccountId,
            @RequestParam(value = "createDept") String creatorDept,
            @RequestParam(value = "questionContent") String questionContent,
            @RequestParam(value = "questionType") String questionType) {
        userQuestionsService.saveQuestion(
                images, creatorName, creatorAccountId, creatorDept, questionContent, questionType);
        return ResponseEntity.ok(ResultResponse.ok(null));
    }
}
